var API = API || {};
jQuery(document).ready(function() {



    // Override default error message
    jQuery.validator.messages.required = "";

    // Override generation of error label
    $("#post").validate({
        submitHandler: function(form) {

            $('input[type=submit]', '#post').attr('disabled', 'disabled');
            $('#ajax-loading').css('visibility', 'visible');

            tinymce.triggerSave();

            var form = $("#post");
            var form_data = form.serialize();

            $.ajax({
                url: base_url + 'posts/submit_post',
                dataType: 'json',
                type: "POST",
                data: form_data,
                success: function(data, textStatus, xhr) {
                    jQuery('#ajax-loading').css('visibility', 'hidden');

                    if (data.action == true) {

                        jQuery('.loader').html(data.msg);

                        // REDIRECT
                        function promo_show() {
                            window.location.href = base_url + 'posts/view_all?post_type=' + post_type;
                        }
                        window.setTimeout(function() {
                            promo_show();
                        }, 1001);

                    } else {

                        $('#post').removeAttr('disabled');
                        alert(data.msg)
                    }
                }
            });

        }
    });

    tinymce.init({
        selector: "textarea",
        theme: "modern",
        plugins: [
            "autolink lists link image print preview hr anchor pagebreak",
            "searchreplace wordcount visualblocks code",
            "media nonbreaking save table contextmenu directionality",
            "template paste "
        ],
        toolbar: "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist outdent indent | template | link",
        menu: {
            file: {title: 'File', items: 'newdocument'},
            edit: {title: 'Edit', items: 'undo redo | cut copy paste | selectall'},
            format: {title: 'Format', items: 'bold italic underline strikethrough superscript subscript | formats | removeformat'},
            table: {title: 'Table', items: 'inserttable tableprops deletetable cell row column'}
        },
        templates: [
            {
                title: "Info - Crnogorski",
                url: API.Data.base_url + "assets/TINYMCE/object_me.html",
                description: "Information"
            },
            {
                title: "Info - English",
                url: API.Data.base_url + "assets/TINYMCE/object_en.html",
                description: "Information"
            }
        ],
        content_css: base_url + "assets/css/editor-style.css",
        wpautop: true,
        apply_source_formatting: false,
        extended_valid_elements: "article[*],aside[*],audio[*],canvas[*],command[*],datalist[*],details[*],embed[*],figcaption[*],figure[*],footer[*],header[*],hgroup[*],keygen[*],mark[*],meter[*],nav[*],output[*],progress[*],section[*],source[*],summary,time[*],video[*],wbr",
        remove_linebreaks: true,
        gecko_spellcheck: true,
        keep_styles: false,
        entities: "38,amp,60,lt,62,gt",
        accessibility_focus: true,
        paste_remove_styles: true,
        paste_remove_spans: true,
        paste_strip_class_attributes: "all",
        paste_text_use_dialog: true,
        relative_urls: false,
        remove_script_host: false,
        convert_urls: false,
        media_strict: false
    });



    $('input[type=submit]', '#post').removeAttr('disabled');


});